<?php
App::uses('AppController', 'Controller');
class ReservationsController extends AppController {

	public function move_room($id = null){
		$this->loadModel('Room');
		$list_rooms = $this->Room->find('list');
	}

	public function list_guest($id = null){
		$date_current = date('Y-m-d H:i:s');
		$data = $this->Reservation->find("all", array(
			"conditions" => array("AND" => array("Reservation.rooms_id" => $id, "Reservation.status" => 2, "Reservation.start >=" => $date_current)
			)
		));
		$this->set(compact('data'));
	}

	public function unset_book(){
		$this->autoRender = false;
		$data = json_decode($_GET["data"]);
		$array_id = array();
		$array_id = explode(",", $data->id);
		$this->Reservation->deleteAll(array('Reservation.guest_id' => $array_id, 'Reservation.status' => 2), false);
		$check_book = $this->Reservation->find('list', array(
			'conditions' => array('Reservation.guest_id' => $array_id),
			'fields' => array('Reservation.guest_id')
		));
		if(count($check_book) == 0){
			$a = $array_id;
		}else{
			$a = array_diff($array_id,$check_book);
		}
		$this->loadModel('Guest');
		$this->Guest->updateAll(
		    array('Guest.status' => 0),
		    array('Guest.id' => $a)
		);

	}

	public function check_out($id = null){
		$data = $this->Reservation->find('all', array(
			'conditions' => array('AND' => array('Reservation.guest_id' => $id, 'Reservation.status' => 1)),
			'fields' => array('Reservation.rooms_id'),
			'order' => array('Reservation.rooms_id' => 'ASC')
		));
		$this->set(compact('data'));
	}

	public function list_guest_cur($id = null){
		$date_current = date('Y-m-d H:i:s');
		$data = $this->Reservation->find("all", array(
			"conditions" => array("AND" => array("Reservation.rooms_id" => $id, "Reservation.status" => 1))
		));
		$this->set(compact('data'));
	}

	public function del_guest_book($id = null){
		$this->loadModel('Guest');
		$this->Guest->id = $id;
		if (!$this->Guest->exists()) {
			return $this->redirect(array('controller' => 'pages','action' => 'home'));
		}
		$this->Guest->delete();
		$this->Reservation->query("DELETE FROM reservation WHERE guest_id = {$id}");
		return $this->redirect(array('controller' => 'pages','action' => 'home'));
	}

	public function delete($id = null){
		$this->Reservation->deleteAll(array('Reservation.guest_id' => $id, 'Reservation.status' => 2), false);
		$chk = $this->Reservation->find('first', array('conditions' => array('Reservation.guest_id' => $id)));
		if(!$chk){
			$this->loadModel('Guest');
			$this->Guest->query("UPDATE guests SET status = 0 WHERE id={$id} ");
		}
		return $this->redirect(array('controller' => 'pages','action' => 'home'));
	}

	public function book($id = null){
		$this->loadModel('Room');
		$room = $this->Room->find('list');
		$this->set(compact('room','id'));
		if ($this->request->is('post')) {
			$data = $this->request->data;
			$this->loadModel('Guest');
			$this->Guest->create();
			$this->Guest->set(array(
			    'name' => $data['Reservation']['name_guest'],
			    'cmt' => $data['Reservation']['cmt'],
			    'status' => 1
			));
			$this->Guest->save();
			$id_guest = $this->Guest->getLastInsertId();
			$data['Reservation']['start'] = $data['Reservation']['start'].' '.$data['Reservation']['ip'].':'.$data['Reservation']['is'].':00';
			$data['Reservation']['end'] = $data['Reservation']['end'].' '.$data['Reservation']['op'].':00:00';
			$data['Reservation']['guest_id'] = $id_guest;
			$data['Reservation']['note'] = trim($data['Reservation']['note']);
			foreach ($data['Reservation']['rooms_id'] as $key => $value) {
				$data['Reservation']['rooms_id'] = $value;
				$this->Reservation->create();			
				$this->Reservation->save($data);	
				$id = $this->Reservation->getLastInsertId();
				if($data['Reservation']['status']==1){
					$this->Reservation->query("INSERT INTO bills (start_date,prepay,sale,reservations_id,rooms_id) VALUE ('".$data['Reservation']['start']."','".$data['Reservation']['prepay']."','".$data['Reservation']['sale']."','".$id."','".$value."')");
				}
			}
			$this->redirect(array('controller' => 'pages', 'action' => 'home'));
		}
	}

	public function received($id = null){
		if ($this->request->is('post')) {
			$data = $this->request->data;
			$this->loadModel('Guest');
			$this->Guest->query("UPDATE guests SET status = 1 WHERE id = '".$data['Reservation']['guest_id']."' ");
			$this->Reservation->create();
			$data['Reservation']['start'] = $data['Reservation']['start'].' '.$data['Reservation']['ip'].':'.$data['Reservation']['is'].':00';
			$data['Reservation']['end'] = $data['Reservation']['end'].' '.$data['Reservation']['op'].':00:00';
			$data['Reservation']['note'] = trim($data['Reservation']['note']);
			foreach ($data['Reservation']['rooms_id'] as $key => $value) {
				$data['Reservation']['rooms_id'] = $value;
				$this->Reservation->saveAll($data);
			}
			$this->redirect(array('controller' => 'pages', 'action' => 'home'));
		}else{
			$this->loadModel('Room');
			$rooms = $this->Room->find('list');
			$name = $_GET['name'];
			$cmt = $_GET['cmt'];
			$this->set(compact('guest', 'rooms', 'name', 'id', 'cmt'));
		}
	}
	
	public function find_room(){
		$this->autoRender = false;
		$data = $_POST["data"];
		$in = $_POST['data']['Reservation']['date_in'].' '.$_POST['data']['Reservation']['h_in'].':'.$_POST['data']['Reservation']['p_in'].':00';
		$out = $_POST['data']['Reservation']['date_out'].' '.$_POST['data']['Reservation']['h_out'].':'.$_POST['data']['Reservation']['p_out'].':00';
		
		$query = "SELECT reservation.rooms_id FROM reservation WHERE (start >= '".$in."' AND end <= '".$out."') OR (start <= '".$out."' AND end >= '".$out."' ) OR (start <= '".$in."' AND end >= '".$in."' )";
		$room_id = $this->Reservation->query($query);
		$arr = array();
		foreach ($room_id as $v1) {
			if(in_array($v1['reservation']['rooms_id'], $arr) || $v1['reservation']['rooms_id'] == ""){
				continue;
			}
			$temp = array();
			if(strlen($v1['reservation']['rooms_id']) > 1){
				$temp = explode(',', $v1['reservation']['rooms_id']);
				foreach ($temp as $v) {
					if(in_array($v, $arr)){
						continue;
					}
					array_push($arr, $v);
				}
			}else{
				array_push($arr,$v1['reservation']['rooms_id']);
			}
		}
		$this->loadModel('Room');
		$result = $this->Room->find('list', array('conditions' => array('Room.id !=' => $arr)));
		echo json_encode($result);
	}

	public function list_room($id = null){
		$this->loadModel('Room');
		$this->autoRender = false;
		if($id == 1){
			$room_id = $this->Reservation->find('all', array(
				'conditions' => array('Reservation.status' => 1),
				'fields' => array('Reservation.rooms_id')
			));
			$arr = array();
			foreach ($room_id as $v1) {
				array_push($arr,$v1['Reservation']['rooms_id']);
			}
			$result = $this->Room->find('list', array('conditions' => array('Room.id !=' => $arr)));
		}else{
			$result = $this->Room->find('list');
		}
		echo json_encode($result);
	}



}
